﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMS.Entity;

namespace CMS.DAL
{
    /// <summary>
    /// 经营综述
    /// </summary>
    public class BussinessProfileDAL
    {
        public BussinessProfile GetOne(string stock, DateTime endDate)
        {
            string sql = "select * from BussinessProfile where Stock=@Stock and EndDate=@EndDate";
            Tools tool = new Tools();
            tool.AddSqlParameter("Stock", System.Data.SqlDbType.VarChar, stock);
            tool.AddSqlParameter("EndDate", System.Data.SqlDbType.DateTime, endDate);
            return SqlHelper.GetDataByColumns<BussinessProfile>(DBConn.gaInfo, sql, tool.GetSqlParameters());
        }

        public void Add(BussinessProfile c)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("INSERT INTO [BussinessProfile]");
            sb.AppendLine("           ([Stock]");
            sb.AppendLine("           ,[EndDate]");
            sb.AppendLine("           ,[Content]");
            sb.AppendLine("           ,[RecTime])");
            sb.AppendLine("     VALUES");
            sb.AppendLine("           (@Stock");
            sb.AppendLine("           ,@EndDate");
            sb.AppendLine("           ,''");
            sb.AppendLine("           ,@RecTime)");
            var sps = SqlHelper.ObjectToSqlParameters(c, new string[] { "Content" });
            SqlHelper.ExecuteNonQuery(DBConn.gaInfo, System.Data.CommandType.Text, sb.ToString(), sps);
            UpdateText(c.Stock, c.EndDate, c.Content);
        }

        void UpdateText(string stock, DateTime endDate, string content)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("DECLARE @val varbinary(16)");
            sb.AppendLine("SELECT @val = TEXTPTR(Content) FROM BussinessProfile where [Stock]=@Stock and [EndDate]=@EndDate");
            sb.AppendLine("WRITETEXT  BussinessProfile.Content @val @Content");
            Tools tool = new Tools();
            tool.AddSqlParameter("Stock", System.Data.SqlDbType.VarChar, stock);
            tool.AddSqlParameter("EndDate", System.Data.SqlDbType.DateTime, endDate);
            tool.AddSqlParameter("Content", System.Data.SqlDbType.Text, content);
            SqlHelper.ExecuteNonQuery(DBConn.gaInfo, System.Data.CommandType.Text, sb.ToString(), tool.GetSqlParameters());
        }

        public void Update(BussinessProfile c)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("UPDATE [BussinessProfile]");
            sb.AppendLine("   SET [RecTime] = @RecTime");
            sb.AppendLine("      ,[ModTime] = @ModTime");
            sb.AppendLine(" WHERE [Stock]=@Stock and [EndDate]=@EndDate");
            var sps = SqlHelper.ObjectToSqlParameters(c, new string[] { "Content" });
            SqlHelper.ExecuteNonQuery(DBConn.gaInfo, System.Data.CommandType.Text, sb.ToString(), sps);
            UpdateText(c.Stock, c.EndDate, c.Content);
        }

    }
}
